package com.zjl.学习.第05章_作业控制.A_作业的启动;

import com.zjl.springbatch.util.OutColour;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.concurrent.TimeUnit;

/**
 * 测试的启动，不会执行sql语句保存
 */
@SpringBootTest(classes = A1_SpringBoot启动.class)
public class A2_Spring单元测试启动 {
    //job调度器
    @Autowired
    private JobLauncher jobLauncher;
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    public Tasklet taskletTest(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                OutColour.out.printlnRed("Hello SpringBatch....");
                TimeUnit.SECONDS.sleep(100);
                return RepeatStatus.FINISHED;
            }
        };
    }
    public Step  stepTest(){
        TaskletStep step1 = stepBuilderFactory.get("stepTest")
                .tasklet(taskletTest())
                .build();
        return step1;
    }
    //定义作业
    public Job jobTest(){
        Job job = jobBuilderFactory.get("jobTest")
                .start(stepTest())
                .build();
        return job;
    }

    @Test
    public void testStart() throws Exception{
        //job作业启动
        //参数1：作业实例，参数2：作业运行携带参数
        jobLauncher.run(jobTest(), new JobParameters());
    }
}
